/**
 * Created by JetBrains WebStorm.
 * User: nhuang
 * Date: 12-4-6
 * Time: 下午2:34
 * To change this template use File | Settings | File Templates.
 */
;
(function ($) {
    $.fn.Tabs = function (options) {
        var defaults = {
            event:'mouseover',
            timeDelay:false,
            timeout:200,
            autoRunShow:false,
            autoTime:2000,
            menuCurrent:'current',
            animateShow:'fadeIn',
            animateTime:500,
            vertical:false,
            verWidth:300,
            norHeight:200,
            callback:null    // 回调函数
        };
        var opts = $.extend(defaults, options);

        this.each(function(){
            var self = $(this),
                boxContent=self.children('div.tab_box'),
                tabBox = boxContent.children('div'), //tabs内容
                menu = self.children('ul.tab_menu'),  //tabs标题栏
                items = menu.find('li'),  //tabs标题
                len = items.length,
                menuNormal=items.width()*parseInt(len),
                menuVertical=items.height()*parseInt(len),
                timer;

            function tabHandle(elem) {      //动作函数
                elem.addClass(opts.menuCurrent).siblings('li').removeClass(opts.menuCurrent);
                tabBox.eq(elem.index())[opts.animateShow](opts.animateTime).siblings('div').hide();
            }

            function delay(elem, time) {    //是否delay，调用动作函数
                if (opts.timeDelay) {
                    setTimeout(function () {
                        tabHandle(elem);
                    }, time)
                } else {
                    tabHandle(elem)
                }
            }

            function start() {      //是否自动切换，调用自动切换函数
                if (!opts.autoRunShow) return;
                timer = setInterval(autoRun, opts.autoTime);
            }

            function autoRun() {        //自动切换函数
                var current = menu.find('li.'+opts.menuCurrent),
                    firstItem = items.eq(0),
                    index = current.index() + 1,
                    item = (index === len )? firstItem : current.next('li'),// index==len,trun to firstTtem;or next one
                    i = (index === len) ? 0 : index;//

                current.removeClass(opts.menuCurrent);
                item.addClass(opts.menuCurrent);
                tabBox.eq(i)[opts.animateShow](opts.animateTime).siblings('div').hide();
            }

            items.bind(opts.event, function () {        //绑定动作how
                delay($(this), opts.timeout);
                if (opts.callback) {
                    opts.callback.call(self);
                }
            });

            if (opts.autoRunShow) {            //自动运行时鼠标动作的影响
                start();
                self.hover(function () {
                    clearInterval(timer);
                    timer = undefined;
                }, function () {
                    start();
                });
            }
            if(opts.vertical){
                items.css("clear","both");
                menu.css("float","left");
                self.height(menuVertical).width(opts.verWidth);
            }else{
                self.width(menuNormal).height(opts.norHeight);
            }
    })
}
})(jQuery);
